# -*- coding: utf-8 -*-

#    Copyright (C) 2005, 2006, 2007 2008, 2009, 2010, 2011 by Promotux
#                       di Francesco Meloni snc - http://www.promotux.it/

#    Author: Francesco Meloni  <francesco@promotux.it>
#    This file is part of Promogest.

#    Promogest is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.

#    Promogest is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with Promogest.  If not, see <http://www.gnu.org/licenses/>.

from werkzeug.exceptions import NotFound
from promogest.lib.webutils import *
from promogest.pages.login import login, logout, prelogin
from promogest.lib.page import Page
from promogest.pages.siteAdmin import siteAdminn
#from promogest.pages.siteAdmin.userAction import user
#from core.pages.siteAdmin.roleAction import role
from promogest.pages.staticPagesAction import staticPages
#from core.pages.siteAdmin.moduloAction import module
#from core.pages.siteAdmin.feedAction import feed
#from core.pages.siteAdmin.portfolioAction import portfolio
#from core.pages.siteAdmin.fileAction import fille
#from core.pages.siteAdmin.serviceAction import service
#from core.pages.siteAdmin.faqAction import faq
#from core.pages.siteAdmin.panAction import pan
#from core.pages.siteAdmin.staticNewsAction import news
#from core.pages.siteAdmin.canoneAction import canone
#from core.pages.siteAdmin.menuItemAction import menuItem
#from core.pages.siteAdmin.companyAction import company
#from core.pages.siteAdmin.swAction import sw
#from core.pages.siteAdmin.olsAction import ols
#from core.pages.siteAdmin.installcodeAction import installcode
#from promogest.pages.siteAdmin.configuration import *
#from core.pages.siteAdmin.subdomainAction import subDomain
#from core.pages.siteAdmin.setconfAction import setConf

""" PARTE RELATIVA AL SITEADMIN """

@expose("/siteAdmin")
@expose("/<subdomain>/siteAdmin")
def siteAdmi(req, subdomain=""):
    subdom = Subdomain().select(name = subdomain.lower() , batchSize=None)
    if subdom:
        subdomain = subdom[0].name
    if hasAction(req,action=[1,2,3,4,5]): # adminrole
        return siteAdminn(req, SUB=subdomain)
    elif hasAction(req,action=[1,2]): #utente
        host_url= "/"+subdomain
        return Page(req).redirect(host_url)
    else:
        print " L'USER NON HA ABBASTANZA DIRITTI DI ACCESSO"
        Session(req).destroy()
        pageData = {'file' : 'not_found'}
        return Page(req).render(pageData)

@expose("/siteAdmin/<action>")
def siteAdminAction(req, subdomain=None, action=None):
    if hasAction(req,action=[1,2,3,4,5]):
    #if True == True:
        print "AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"
        #if action.upper() =="THEME_SETUP":
            #return theme_setup(req,subdomain=subdomain, action=action)
        #elif action.upper() =="SETUP":
            #return mainSetup(req,subdomain=subdomain, action=action)

        #if "USER" in action.upper():
            #return user(req,subdomain=subdomain, action=action)
        #if "CANONE" in action.upper():
            #return canone(req,subdomain=subdomain, action=action)
        #if "NEWS" in action.upper():
            #return news(req,subdomain=subdomain, action=action)
        #if "MENUITEM" in action.upper():
            #return menuItem(req,subdomain=subdomain, action=action)
        #if "SETCONF" in action.upper():
            #return setConf(req,subdomain=subdomain, action=action)
        #if "SUBDOMAIN" in action.upper():
            #return subDomain(req,subdomain=subdomain, action=action)
        #if "OLS" in action.upper():
            #return ols(req,subdomain=subdomain, action=action)
        #if "ROLE" in action.upper():
            #return role(req,subdomain=subdomain, action=action)
        if "STATICPAGES" in action.upper():
            print "FHHHHHHHHHHHFHFHFHFHFHFH"
            return staticPages(req,subdomain=subdomain, action=action)
        #if "SW" in action.upper():
            #return sw(req,subdomain=subdomain, action=action)
        #if "COMPANY" in action.upper():
            #return company(req,subdomain=subdomain, action=action)
        #if "MODULE" in action.upper():
            #return module(req,subdomain=subdomain, action=action)
        #if "FEED" in action.upper():
            #return feed(req,subdomain=subdomain, action=action)
        #if "PORTFOLIO" in action.upper():
            #return portfolio(req,subdomain=subdomain, action=action)
        #if "SERVICE" in action.upper():
            #return service(req,subdomain=subdomain, action=action)
        #if "FAQ" in action.upper():
            #return faq(req,subdomain=subdomain, action=action)
        #if "PAN" in action.upper():
            #return pan(req,subdomain=subdomain, action=action)
        #if "INSTALLCODE" in action.upper():
            #return installcode(req,subdomain=subdomain, action=action)
        #if "FILE" in action.upper():
            #return fille(req,subdomain=subdomain, action=action)
    else:
        pageData = {'file' : 'not_found'}
        return Page(req).render(pageData)
